package com.sise.controller;

import com.sise.service.RedisService;
import com.sise.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Auther: Gentle
 * @Date: 2019/3/30 22:43
 * @Description:
 */
@RestController
@RequestMapping(value = "api/redis/")
public class RedisController {
    @Autowired
    RedisService redisService;

    @PostMapping(value = "set")
    public Integer set(@RequestParam(value = "key") String key,
                      @RequestParam(value = "value") String value,
                      @RequestParam(value = "time",required = false) Integer time){
        if (time==null){
            redisService.set(key,value);
        }else {
            redisService.set(key,value,time);
        }
        return 0;
    }

    @PostMapping(value = "hset")
    public Integer hset(@RequestParam(value = "key") String key,
                      @RequestParam(value = "value") String value,
                      @RequestParam(value = "field") String field,
                      @RequestParam(value = "time",required = false) Integer time){
        if (time==null){
            redisService.hashSet(key,field,value);
        }else {
            redisService.hashSet(key,field,value,time);
        }
        return 0;
    }


    @PostMapping(value = "get")
    public String get(@RequestParam(value = "key") String key){

        return redisService.get(key);
    }


    @PostMapping(value = "incr")
    public Long incr(@RequestParam(value = "key") String key,@RequestParam(value = "count",required = false) Integer count){

        if (count==null){
            return redisService.incr(key);
        }
        return redisService.incr(key,count);

    }

    @PostMapping(value = "decr")
    public Long decr(@RequestParam(value = "key") String key,@RequestParam(value = "count",required = false) Integer count){

        if (count==null){
            return redisService.decr(key);
        }
        return redisService.decr(key,count);
    }

    @PostMapping(value = "getMany")
    public List<String> getMany(@RequestParam(value = "keys") String keys){
        List<String> strings = JsonUtils.jsonTolist(keys, String.class);
        return redisService.getMany(strings);
    }

}
